export default {
  // 必须得在引入该组件的地方增加 tableRefName 和 idKey
  data() {
    return {
      multipleSelectionAll: [] // 所有页面选中的数据
    };
  },
  created() {
    if (!this.tableRefName || (!this.idKey && this.idKey !== 0)) {
      console.error('tableRefName 或 idKey 未设置');
    }
  },
  methods: {
    selectTable(selection, row) {
      const idKey = this.idKey;
      const primaryId = this.primaryId;
      let isSelect; // 判断当前是选中还是取消选中
      if (row) {
        // 单选
        const id = row[idKey] || row[primaryId];
        isSelect = selection.some(i => i[idKey] === id || i[primaryId] === id);
      } else {
        // 多选按钮
        isSelect = selection.length > 0;
      }
      if (isSelect) {
        this.addSelect(selection);
      } else {
        this.removeSelect(selection);
        if (this.isSelectedOrder) {
          this.$emit('relatedCancel', row);
        }
      }
      if (this.tableSelectionChange) this.tableSelectionChange(this.multipleSelectionAll);
      if (this.selectCallback) {
        this.$emit('onSelectChange', this.tableRefName, this.multipleSelectionAll);
      }
    },
    selectAllTable(selection, tableName) {
      if (this.isSelectedOrder) {
        this.$emit('relatedMulCancel', tableName);
      } else {
        this.selectTable(selection);
      }
    },
    addSelect(selection) {
      if (this.multipleSelectionAll.length === 0) {
        this.multipleSelectionAll = selection;
        return;
      }
      const selectIds = this.multipleSelectionAll.map(i => i[this.primaryId] || i[this.idKey]);
      const concatOrder = selection.filter(i => !selectIds.includes(i[this.idKey]));
      this.multipleSelectionAll = this.multipleSelectionAll.concat(concatOrder);
    },
    removeSelect(selection) {
      const idKey = this.idKey;
      const primaryId = this.primaryId;
      const tableData = this.$refs[this.tableRefName || 'tableRef'].data;
      const currentPageSelectIds = selection.map(i => i[idKey] || i[primaryId]);
      const removeIds = [];
      tableData.forEach(i => {
        if (!currentPageSelectIds.includes(i[idKey]) && !currentPageSelectIds.includes(i[primaryId])) {
          removeIds.push(i[idKey]);
        }
      });
      this.multipleSelectionAll = this.multipleSelectionAll.filter(i => {
        return !removeIds.includes(i[idKey]) && !removeIds.includes(i[primaryId]);
      });
    },
    clearMixinSelection() {
      this.$nextTick(() => {
        this.multipleSelectionAll = [];
        if (this.$refs[this.tableRefName || 'tableRef']) this.$refs[this.tableRefName || 'tableRef'].clearSelection();
        if (this.tableSelectionChange) this.tableSelectionChange(this.multipleSelectionAll);
      });
    },
    setSelectRow(tableData) { // 翻页时自动选中
      this.$nextTick(() => {
        if (!this.multipleSelectionAll || this.multipleSelectionAll.length <= 0) {
          return;
        }
        // 标识当前行的唯一键的名称
        const idKey = this.idKey;
        const primaryId = this.primaryId || '';
        const selectAllIds = this.multipleSelectionAll.map(row => row[primaryId] || row[idKey]);
        this.$refs[this.tableRefName || 'tableRef'] && this.$refs[this.tableRefName || 'tableRef'].clearSelection();
        for (var i = 0; i < tableData.length; i++) {
          if (selectAllIds.indexOf(tableData[i][idKey]) >= 0) {
            if (this.isUmyTable) {
              this.$refs[this.tableRefName || 'tableRef'].toggleRowSelection([{ row: tableData[i], selected: true }]);
            } else {
              this.$refs[this.tableRefName || 'tableRef'].toggleRowSelection(tableData[i], true);
            }
          }
        }
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (this.multipleSelectionAll && this.multipleSelectionAll.length > 0) {
        const ids = this.multipleSelectionAll.map(i => i.id);
        return ids.includes(row.id) ? 'table-selected' : '';
      }
      return '';
    }
  }
};
